package com.nf.health.app.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.WebViewActivity;
import com.nf.health.app.models.BannerBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.widget.InfinitePagerAdapter;

import java.util.ArrayList;

/**
 * Created by Jason on 2016/3/16.
 */
public class AdsPagerAdapter extends InfinitePagerAdapter {

    private Activity context;
    private ArrayList<BannerBean> list;
    private LayoutInflater inflater;


    public AdsPagerAdapter(Activity context, ArrayList<BannerBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup container) {
        //final ImageView imageView = (ImageView) inflater.inflate(R.layout.item_infinite_viewpager, container, false);
        ViewHolder holder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.activity_bennary, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_bennary);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
       //ImageLoaderUtils.getinstance(context).getImage(holder.imageView, list.get(position).getImg(), R.drawable.default_square_image);
        //ImageView imageView = null;

        String imgUrl = list.get(position).getImgurl();
        final String url = list.get(position).getUrl();
        Glide.with(context)
                .load(imgUrl)
                .placeholder(R.drawable.default_square_image)
                .into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(url)) {
                    Bundle bundle = new Bundle();
                    bundle.putString(WebViewActivity.KEY_URL, url);
                    ActivityUtils.showActivity(context, WebViewActivity.class, bundle);
                }
            }
        });
       /* Resources res = context.getResources();
        int[] tab1 = res.getIntArray(R.array.image_array_ids);*/
      /*switch (position){
            case 0:
            setTagValues(holder.imageView,position,R.id.tag_0);
                break;
            case 1:
                setTagValues(holder.imageView,position,R.id.tag_1);
                break;
            case 2:
                setTagValues(holder.imageView,position,R.id.tag_2);
                break;
            case 3:
                setTagValues(holder.imageView,position,R.id.tag_3);
                break;
            case 4:
                setTagValues(holder.imageView,position,R.id.tag_4);
                break;
            case 5:
                setTagValues(holder.imageView,position,R.id.tag_5);
                break;
            case 6:
                setTagValues(holder.imageView,position,R.id.tag_6);
                break;
            case 7:
                setTagValues(holder.imageView,position,R.id.tag_7);
                break;
            case 8:
                setTagValues(holder.imageView,position,R.id.tag_8);
                break;
            case 9:
                setTagValues(holder.imageView,position,R.id.tag_9);
                break;
            case 10:
                setTagValues(holder.imageView,position,R.id.tag_10);
                break;
        }*/

        return convertView;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }
    public void setTagValues(ImageView imageView,int position,int tag){
        if(imageView.getTag(tag) == null){
            imageView.setTag(tag,list.get(position).getImgurl());
            Glide.with(context)
                    .load(list.get(position).getImgurl())
                    .placeholder(R.drawable.default_square_image)
                    .into(imageView);
        }else{
            String getTag = (String) imageView.getTag(tag);
            /*if(!getTag.equals(list.get(position).getImg())){
                Glide.with(context)
                        .load(list.get(position).getImg())
                        .placeholder(R.drawable.default_square_image)
                        .into(imageView);
            }*/
        }
    }
    private class ViewHolder{
        ImageView imageView;
    }
   public void setMy(ArrayList<BannerBean> list){
      // getItemCount() = getCount();
       this.list = list;
       super.notifyDataSetChanged();
    }

}
